package cn.com.coding.app;

import cn.com.coding.common.lang.ResT;
import cn.com.coding.system.domin.SysMenuEntity;
import cn.com.coding.system.domin.SysRoleMenuEntity;
import cn.com.coding.system.domin.SysUserEntity;
import cn.com.coding.system.domin.dto.SysMenuDto;
import cn.com.coding.system.service.SysMenuService;
import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.security.Principal;
import java.util.List;
import java.util.Map;


/**
 * @author mxz
 */
@RestController
@RequestMapping("cat/sysmenu")
public class SysMenuController extends BaseController {

    @Autowired
    private SysMenuService sysMenuService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public ResT list(@RequestParam Map<String, Object> params) {
        List<SysMenuEntity> list = sysMenuService.tree();

        return ResT.ok().setData(MapUtil.builder().put("menuList", list).build());
    }

    /**
     * 菜单列表
     */
    @GetMapping("/nav")
    public ResT all(Principal principal) {
        SysUserEntity sysUserEntity = sysUserService.getByUserName(principal.getName());

        // 获取权限信息字符串
        String userAuthorityInfo = sysUserService.getUserAuthorityInfo(sysUserEntity.getPid());
        String[] tokenizeToStringArray = StringUtils.tokenizeToStringArray(userAuthorityInfo, ",");

        // 获取导航栏信息
        List<SysMenuDto> currentUserNav = sysMenuService.getCurrentUserNav();


        return ResT.ok().setData(MapUtil.builder()
                .put("authority", tokenizeToStringArray)
                .put("nav", currentUserNav)
                .build());
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{pid}")
    public ResT info(@PathVariable("pid") String pid) {
        SysMenuEntity sysMenu = sysMenuService.getById(pid);

        return ResT.ok().put("sysMenu", sysMenu);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public ResT save(@Validated @RequestBody SysMenuEntity sysMenu) {


        sysMenuService.save(sysMenu);

        return ResT.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public ResT update(@RequestBody SysMenuEntity sysMenu) {
        sysMenuService.updateById(sysMenu);


        // 清除所有与菜单相关联的权限缓存
        sysUserService.clearUserAuthorityInfoByMenuPid(sysMenu.getPid());

        return ResT.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete/{pid}")
    public ResT delete(@PathVariable("pid") String pid) {

        int count = sysMenuService.count(new QueryWrapper<SysMenuEntity>().eq("parent_pid", pid));
        if (count > 0) {
            return ResT.error().setData(MapUtil.builder()
                    .put("msg", "请先删除子菜单")
                    .build());
        }

        // 清除所有与菜单相关联的权限缓存
        sysUserService.clearUserAuthorityInfoByMenuPid(pid);

        sysMenuService.removeById(pid);

        // 同步删除中间关联表
        sysRoleMenuService.remove(new QueryWrapper<SysRoleMenuEntity>().eq("menu_pid", pid));

        return ResT.ok();
    }

}
